﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace _6_05_多窗体实现数据增删改查_窗体传值_
{
    public partial class AddFrm : Form
    {
        public AddFrm()
        {
            InitializeComponent();
        }
        //private void BindData() { 

        //}

        private void button1_Click(object sender, EventArgs e)
        {
            string conn = "server=.;database=TestDB;uid=sa;pwd=200234.A";
            SqlConnection sqlconn = new SqlConnection(conn);
            sqlconn.Open();
            string tempcmd = "select * from  Member where MemberAccount =@MemberAccount";
            SqlDataAdapter dap = new SqlDataAdapter(tempcmd, sqlconn);
            dap.SelectCommand.Parameters.Add(new SqlParameter("MemberAccount", this.textBox1.Text));
            DataTable dt = new DataTable();
            dap.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                MessageBox.Show("账号已存在");
                sqlconn.Close();
                return;
            }
            //string cmd = $"insert into Member(MemberAccount,MemberPwd,MemberName,MemberPhone) " +
            //    $"values('{this.textBox1.Text}','{this.textBox2.Text}','{this.textBox3.Text}','{this.textBox4.Text}')";
            string cmd = "insert into Member(MemberAccount,MemberPwd,MemberName,MemberPhone) " +
                "values(@MemberAccount,@MemberPwd,@MemberName,@MemberPhone)";
            SqlCommand sqlcmd = new SqlCommand(cmd, sqlconn);
            sqlcmd.Parameters.Add(new SqlParameter("MemberAccount", this.textBox1.Text));
            sqlcmd.Parameters.Add(new SqlParameter("MemberPwd", this.textBox2.Text));
            sqlcmd.Parameters.Add(new SqlParameter("MemberName", this.textBox3.Text));
            sqlcmd.Parameters.Add(new SqlParameter("MemberPhone", this.textBox4.Text));
            int count = sqlcmd.ExecuteNonQuery();
            if (count > 0)
            {
                MessageBox.Show("添加成功");
            }
            else
            {
                MessageBox.Show("添加失败");
            }
            //BindData();

            sqlconn.Close();
            ((Form1)(this.Owner)).BindData();
            this.Close();
        }
    }
}
